package com.kiss.rpc.channelhandler;

import com.kiss.rpc.enumeration.RequestType;
import com.kiss.rpc.transport.message.RequestPayload;
import com.kiss.rpc.transport.message.RpcRequest;
import com.kiss.rpc.transport.message.RpcResponse;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

class MyClientHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelActive(ChannelHandlerContext ctx){
        //发送消息到服务端
        RpcRequest rpcRequest = new RpcRequest();
        rpcRequest.setRequestId(1);
        rpcRequest.setRequestType(RequestType.REQUEST.getId());
        rpcRequest.setTimeStamp(System.currentTimeMillis());
        rpcRequest.setCompressType((byte) 1);
        rpcRequest.setSerializeType((byte) 1);
        RequestPayload requestPayload = new RequestPayload();
        requestPayload.setMethodName("doSomething");
        requestPayload.setInterfaceName(Do.class.getName());
        requestPayload.setReturnType(String.class);
        requestPayload.setParametersType(new Class[]{String.class});
        requestPayload.setParametersValue(new Object[]{"hello"});
        rpcRequest.setRequestPayload(requestPayload);
        ctx.writeAndFlush(rpcRequest);
    }

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg){
        //接收服务端发送过来的消息
        if (msg instanceof RpcResponse rpcResponse) {
            System.out.println("收到服务端" + rpcResponse.getBody() +  "的消息");
        }
    }
}
